<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>审批管理</title>
    <link href="/component/pear/css/pear.css" rel="stylesheet"/>
    <style>
        /*使下拉列表框不被遮挡*/
        .layui-table-cell {
            overflow: visible !important;
        }

        /*使列表框与表格单元格重合*/
        td .layui-form-select {
            margin-top: -3px;
            margin-left: -15px;
            margin-right: 5px;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">文件名:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">借阅人:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="approval-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="approval-table" lay-filter="approval-table"></table>
    </div>
</div>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let cols = [
            [
                {title: '机柜编号', field: 'number', align: 'center', width: 120},
                {title: '文件名', field: 'file_name', align: 'center'},
                {title: '借阅人', field: 'borrowers', align: 'center'},
                {title: '借阅日期', field: 'borrowing_date', align: 'center'},
                {title: '备注', field: 'remarks', align: 'center'},
                {title: '申请日期', field: 'create_time', align: 'center'},
                {
                    title: '操作', align: 'center', width: 130, templet: function (d) {
                        if (d.approval_status == 0) {
                            let opt = '<select name="approvalStatus" id="reuseDegree" class="province-selector" lay-filter="approvalStatus" >\n' +
                                '<option value=""></option>';
                            opt = opt + '<option value="1">已通过</option>\n' +
                                '<option value="2" >未通过</option>\n';
                            opt = opt + '</select>';
                            return opt;
                        } else if (d.approval_status == 1) {
                            return '<span style="color: #36b368">已通过</span>';
                        } else {
                            return '<span style="color: red">未通过</span>';
                        }
                    }, exportTemplet: function (d, obj) {
                        //console.log(obj)
                        // 处理该字段的导出数据
                        console.log(d)
                        console.log(obj)
                        var td = obj.td(this.field); //获取当前 td
                        return td.find('select').val();
                    }
                }
            ]
        ]
        tableIns = table.render({
            elem: '#approval-table',
            url: '/borrowing/records/approval/list',
            page: true,
            cols: cols,
            skin: 'line',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'testReload',
            height: 'full-150',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['testReload'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("testReload", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        })
        form.on('submit(approval-query)', function (data) {
            table.reload('testReload', {where: data.field})
            return false;
        });
        // 刷新按钮
        window.refresh = function () {
            table.reload('testReload');
        }
        // 下拉框审批
        form.on('select(approvalStatus)', function (data) {
            if (data.value == '') {
                return false;
            }
            var tableCache = table.cache['testReload'], // 获得数据表格的缓存数据
                dataIndex = parseInt(data.othis.parents('tr').attr('data-index')),  // 获得变化的下拉列表所在的行index
                lineDate = tableCache[dataIndex];  // 获得数据表格中该行的缓存数据
            $.ajax({
                url: '/borrowing/records/approval/save',
                data: JSON.stringify({'id':lineDate.id,'approval_status':data.value,'file_name':lineDate.file_name,'number':lineDate.number}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.message, {icon: 1, time: 3000}, function () {
                            // window.refresh();
                            layui.table.reload("testReload");
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 3000}, function () {
                            layui.table.reload("testReload");
                        });
                    }
                }
            })
            console.log(lineDate.id)
        });
    })
</script>
</body>
</html>
